﻿using System;

namespace RRRSRoguelike.Managers
{
	/// <summary>
	/// Reads and interprets the keys pressed by the player.
	/// </summary>
	class InputManager
	{

		ConsoleKeyInfo keyInfo;
		ConsoleKeyInfo previousKeyinfo;

		public ConsoleKey ReadKey()
		{
			return Console.ReadKey(true).Key;
		}

		public void ReadKeys()
		{
			previousKeyinfo = keyInfo;

			//Always reads the last key pressed.  If you hit the wrong key, any key not used will cancel if you are quick enough.
			//During the battle there is a one second pause.  If a key is pressed it will be delayed.
			//Add Clearinputstream problem solved.

			//wait for a key to be pressed


			if (Console.KeyAvailable == false)
				keyInfo = new ConsoleKeyInfo();
			else
			{
				//while keys are available loop
				while (Console.KeyAvailable == true)
				{
					//true intercepts the key from appearing in the console.
					keyInfo = Console.ReadKey(true);
				}
			}
		}

		public ConsoleKeyInfo currentKey
		{
			get { return keyInfo; }
		}

		public bool KeyPush(ConsoleKey key)
		{
			//If you press the same key twice, doesn't count as new push.
			if (keyInfo.Key == key && previousKeyinfo.Key == 0)
			{
				return true;
			}
			return false;
		}

		//public bool KeyDown(ConsoleKey key)
		//{

		//    if ( keyInfo.Key == key) 
		//    {    
		//        return true;
		//    }
		//    return false;
		//}

		//Doesn't work the way it should
		//public bool KeyReleased(ConsoleKey key)
		//{
		//    //If you just pressed one key, and now another key is pressed.
		//    if (keyInfo.Key == 0 && previousKeyinfo.Key == key)
		//    {
		//        return true;
		//    }
		//    return false;
		//}

		public static void clearInputStream()
		{
			while (Console.KeyAvailable == true)
			{
				Console.ReadKey();
			}
		}
	}
}
